<?php
class PageInfo {
	public $_pagesize = 10;
	public $_totality;//总数
	public $_pageNum;
	public $_nextpage;//下一页
	public $_uppage;//上一页
	public $_isEnd = false;
	public $_isFirst = false;
	public $_totalityPage;
	public $_start;
	
	
	public function __construct($pageNum,$totality,$pagesize=10){
		$this->_pagesize = $pagesize;
		$this->_pageNum = $pageNum;
		$this->_totality = $totality;
		$this->_start = ($this->_pageNum-1)*$this->_pagesize;
		($this->_start+$this->_pagesize)>=$this->_totality?$this->_isEnd=true:$this->_isEnd=false;
		if($this->_pageNum==1){
			$this->_isFirst= true;
		}
		if ($this->_isEnd){
			$this->_nextpage = $this->_pageNum;
		}else{
			$this->_nextpage = $this->_pageNum+1;
		}
		if($this->_isFirst){
			$this->_uppage = $this->_pageNum;
		}else{
			$this->_uppage = $this->_pageNum-1;
		}
		if ($this->_totality > 0 && ($this->_totality > $this->_pagesize)){
			if(fmod($this->_totality,$this->_pagesize)>0){
				$this->_totalityPage=floor($this->_totality/$this->_pagesize)+1;
			}else{
				$this->_totalityPage=floor($this->_totality/$this->_pagesize);
			}
			
		}else{
			$this->_totalityPage= 1;
		}
		 
	}
	
	function page($array,$pagesize,$current){
		$_return=array();
		/*calculator*/
		$total=ceil(Count($array)/$pagesize);
		$prev=(($current-1)<=0 ? "1":($current-1));
		$next=(($current+1)>=$total ?   $total:$current+1);
		
		$current=($current>($total)?($total):$current);
		
		$start=($current-1)*$pagesize;
		
		
		for($i=$start;$i<($start+$pagesize);$i++){
			array_push($_return,$array[$i]);
		}
		
		return $_return;
	}
	
	
	function showpagination($cp,$s){
		$C = "";
		if($cp!=0){
			if($s==""){
				$C = "&CP=".$cp;
			}else{
				$C = "&CP=".$cp."&S=".$s;
			}
			
		}else{
			if($s==""){
			}else{
				$C = "&S=".$s;
			}
		}
		$showNum = 7;
		$splitNum = 4;
		$show = "";
		if ($this->_totalityPage==1){//只有一页
			$show .= "<span class=\"current\">1</span>";
		}elseif ($this->_isFirst){//首页
			$show .= "<span class=\"current\">1</span>";
			if ($this->_totalityPage>7){//页数大于7页的
				for ($i = 2; $i < 7; $i++) {
					$show .="<a href=\"?P=$i".$C."\">$i</a>";
				}
				$show .="<span class=\"dot\">...</span>";
				$show .="<a href=\"?P=$this->_totalityPage".$C."\">$this->_totalityPage</a>";
			}else{//小于7页
				for ($i = 2; $i < $this->_totalityPage+1; $i++) {
					$show .="<a href=\"?P=$i".$C."\">$i</a>";
				}
			}
			$nen = $this->_pageNum+1;
			$show .="<a id=\"page-next\" href=\"?P=$nen".$C."\">»</a>";
		}elseif ($this->_isEnd){//最后一页
			$upn = $this->_pageNum-1;
			$show .="<a id=\"page-prev\" href=\"?P=$upn".$C."\">«</a>";
			if ($this->_totalityPage>7){//页数大于7
				$show .="<a href=\"?P=1".$C."\">1</a>";
				$show .="<span class=\"dot\">...</span>";
				for ($i = $this->_totalityPage-5; $i < $this->_totalityPage; $i++){
					$show .="<a href=\"?P=$i".$C."\">$i</a>";
				}
				$show .= "<span class=\"current\">$this->_totalityPage</span>";
			}else{
				for ($i = 1; $i < $this->_totalityPage; $i++){
					$show .="<a href=\"?P=$i".$C."\">$i</a>";
				}
				$show .= "<span class=\"current\">$this->_totalityPage</span>";
			}
		}else{//其他页码的
			$upn = $this->_pageNum-1;
			$nen = $this->_pageNum+1;
			$show .="<a id=\"page-prev\" href=\"?P=$upn".$C."\">«</a>";
			if ($this->_totalityPage>7){//页数大于7页的
				if ($this->_pageNum<5){
					for ($i = 1; $i < 7; $i++){
						if ($this->_pageNum==$i){
							$show .= "<span class=\"current\">$i</span>";
						}else{
							$show .="<a href=\"?P=$i".$C."\">$i</a>";
						}
					}
					$show .="<span class=\"dot\">...</span>";
					$show .="<a href=\"?P=$this->_totalityPage".$C."\">$this->_totalityPage</a>";
				}elseif ($this->_pageNum>($this->_totalityPage-4)){
					$show .="<a href=\"?P=1".$C."\">1</a>";
					$show .="<span class=\"dot\">...</span>";
					for ($i = $this->_totalityPage-5; $i < $this->_totalityPage+1; $i++){
						if ($this->_pageNum==$i){
							$show .= "<span class=\"current\">$i</span>";
						}else{
							$show .="<a href=\"?P=$i".$C."\">$i</a>";
						}
					}
				}else{
					$show .="<a href=\"?P=1".$C."\">1</a>";
					$show .="<span class=\"dot\">...</span>";
					for ($i = $this->_pageNum-2; $i < $this->_pageNum+3; $i++){
						if ($this->_pageNum==$i){
							$show .= "<span class=\"current\">$i</span>";
						}else{
							$show .="<a href=\"?P=$i".$C."\">$i</a>";
						}
					}
					$show .="<span class=\"dot\">...</span>";
					$show .= "<a href=\"?P=$this->_totalityPage".$C."\">$this->_totalityPage</a>";
				}
			}else{
				
				for ($i = 1; $i < $this->_totalityPage+1; $i++){
					if ($this->_pageNum==$i){
						$show .= "<span class=\"current\">$i</span>";
					}else{
						$show .="<a href=\"?P=$i".$C."\">$i</a>";
					}
				}
			}
			$show .="<a id=\"page-next\" href=\"?P=$nen".$C."\">»</a>";
		}
		
		$show .="&nbsp;<input type=\"text\" class=\"page_skip\" name=\"jumpto\" id=\"pageJumpto\" size=\"3\" value=\"$this->_pageNum\">&nbsp;";
		$show .="<input  type=\"hidden\" name=\"condition\" id=\"condition\" value=\"$C\" />";
		$show .="<input  type=\"hidden\" name=\"bigest\" id=\"bigest\"value=\"$this->_totalityPage\" />";
		$show .="<button id=\"B_pageJump\" class=\"btn_w\" type=\"button\">GO</button>";
		
		return $show;
	}
	
	/**
	 * 伪静态url使用
	 * Enter description here ...
	 * @param unknown_type $cp
	 * @param unknown_type $s
	 */
	function showpaginationHtml($cp,$s){
		$C = "/CP/0/";
		if($cp!=0){
			if($s==""){
				$C = "/CP/".$cp."/";
			}else{
				$C = "&CP=".$cp."&S=".$s;
			}
			
		}else{
			if($s==""){
			}else{
				$C = "/S/".$s."/";
			}
		}
		$showNum = 7;
		$splitNum = 4;
		$show = "";
		if ($this->_totalityPage==1){//只有一页
			$show .= "<span class=\"current\">1</span>";
		}elseif ($this->_isFirst){//首页
			$show .= "<span class=\"current\">1</span>";
			if ($this->_totalityPage>7){//页数大于7页的
				for ($i = 2; $i < 7; $i++) {
					$show .="<a href=\"".$C."$i\">$i</a>";
				}
				$show .="<span class=\"dot\">...</span>";
				$show .="<a href=\"".$C."$this->_totalityPage\">$this->_totalityPage</a>";
			}else{//小于7页
				for ($i = 2; $i < $this->_totalityPage+1; $i++) {
					$show .="<a href=\"".$C."$i\">$i</a>";
				}
			}
			$nen = $this->_pageNum+1;
			$show .="<a id=\"page-next\" href=\"".$C."$nen\">»</a>";
		}elseif ($this->_isEnd){//最后一页
			$upn = $this->_pageNum-1;
			$show .="<a id=\"page-prev\" href=\"".$C."$upn\">«</a>";
			if ($this->_totalityPage>7){//页数大于7
				$show .="<a href=\"".$C."1\">1</a>";
				$show .="<span class=\"dot\">...</span>";
				for ($i = $this->_totalityPage-5; $i < $this->_totalityPage; $i++){
					$show .="<a href=\"".$C."$i\">$i</a>";
				}
				$show .= "<span class=\"current\">$this->_totalityPage</span>";
			}else{
				for ($i = 1; $i < $this->_totalityPage; $i++){
					$show .="<a href=\"".$C."$i\">$i</a>";
				}
				$show .= "<span class=\"current\">$this->_totalityPage</span>";
			}
		}else{//其他页码的
			$upn = $this->_pageNum-1;
			$nen = $this->_pageNum+1;
			$show .="<a id=\"page-prev\" href=\"".$C."$upn\">«</a>";
			if ($this->_totalityPage>7){//页数大于7页的
				if ($this->_pageNum<5){
					for ($i = 1; $i < 7; $i++){
						if ($this->_pageNum==$i){
							$show .= "<span class=\"current\">$i</span>";
						}else{
							$show .="<a href=\"".$C."$i\">$i</a>";
						}
					}
					$show .="<span class=\"dot\">...</span>";
					$show .="<a href=\"".$C."$this->_totalityPage\">$this->_totalityPage</a>";
				}elseif ($this->_pageNum>($this->_totalityPage-4)){
					$show .="<a href=\"".$C."1\">1</a>";
					$show .="<span class=\"dot\">...</span>";
					for ($i = $this->_totalityPage-5; $i < $this->_totalityPage+1; $i++){
						if ($this->_pageNum==$i){
							$show .= "<span class=\"current\">$i</span>";
						}else{
							$show .="<a href=\"".$C."$i\">$i</a>";
						}
					}
				}else{
					$show .="<a href=\"".$C."1\">1</a>";
					$show .="<span class=\"dot\">...</span>";
					for ($i = $this->_pageNum-2; $i < $this->_pageNum+3; $i++){
						if ($this->_pageNum==$i){
							$show .= "<span class=\"current\">$i</span>";
						}else{
							$show .="<a href=\"".$C."$i\">$i</a>";
						}
					}
					$show .="<span class=\"dot\">...</span>";
					$show .= "<a href=\"".$C."$this->_totalityPage\">$this->_totalityPage</a>";
				}
			}else{
				
				for ($i = 1; $i < $this->_totalityPage+1; $i++){
					if ($this->_pageNum==$i){
						$show .= "<span class=\"current\">$i</span>";
					}else{
						$show .="<a href=\"".$C."$i\">$i</a>";
					}
				}
			}
			$show .="<a id=\"page-next\" href=\"".$C."$nen\">»</a>";
		}
		
		$show .="&nbsp;<input type=\"text\" class=\"page_skip\" name=\"jumpto\" id=\"pageJumpto\" size=\"3\" value=\"$this->_pageNum\">&nbsp;";
		$show .="<input  type=\"hidden\" name=\"condition\" id=\"condition\" value=\"$C\" />";
		$show .="<input  type=\"hidden\" name=\"bigest\" id=\"bigest\"value=\"$this->_totalityPage\" />";
		$show .="<button id=\"B_pageJump\" class=\"btn_w\" type=\"button\">GO</button>";
		
		return $show;
	}
	
}